import { stringify } from 'qs';
import { notification } from 'antd';
import requestInner from '../utils/request';
import config from '../common/config';
import { getCookie } from '../utils/utils';

const request = async (url, params, method) => {
  const result = await requestInner(url, params, method);
  if (!result) {
    return {};
  }
  if (result.code === 401) {
    // window.location.href = `${location.protocol}//${location.host}${location.pathname}#/user/login`;
    window.localStorage.removeItem('current')
    window.location.href = config.trade_login_url;
  }
  if (result.code === 403) {
    notification.error({
      message: `${result.message}`,
      description: '请联系管理员配置该功能权限后再试',
    });
    return result;
  } else {
    return result;
  }
};

let getOptions = function(tokenParam) {
  let options = {};
  return options;
};

async function fileDownload(url, params, filename) {
  const token = localStorage.getItem('admintoken');
  const adminToken = getCookie('adminToken');
  const htk = adminToken ? { adminToken } : {};
  const request = {
    body: JSON.stringify(params),
    method: 'POST',
    headers: {
      'Content-Type': 'application/json;charset=UTF-8',
      Authorization: `Bearer ${token}`,
      ...htk,
    },
  };

  const response = await fetch(url, request);
  const blob = await response.blob();

  const link = document.createElement('a');
  link.download = decodeURIComponent(filename);
  link.style.display = 'none';
  link.href = URL.createObjectURL(blob);
  document.body.appendChild(link);
  link.click();
  URL.revokeObjectURL(link.href);
  document.body.removeChild(link);
}

// login
export async function getCaptchaImage(params) {
  return request(`${config.serveraddress}/admin/getCaptchaImage`, params);
}

export async function accountLogin(params) {
  return request(`${config.serveraddress}/admin/login`, params, 'POST');
}

export async function getAdminInfo(params) {
  return request(`${config.serveraddress}/admin/getAdminInfo`, params,'GET');
}

// 标签管理
export async function tagList(params) {
  return request(`${config.serveraddress}/tag/list`, params, 'GET');
}
export async function tagSave(params) {
  return request(`${config.serveraddress}/tag/save`, params, 'POST');
}
export async function tagDelete(params) {
  return request(`${config.serveraddress}/tag/delete`, params, 'DELETE');
}

export async function tagCategoryListAll(params) {
  return request(`${config.serveraddress}/tag/category/listAll`, params, 'GET');
}
export async function tagCategorySave(params) {
  return request(`${config.serveraddress}/tag/category/save`, params, 'POST');
}
export async function tagCategoryDelete(params) {
  return request(`${config.serveraddress}/tag/category/delete`, params, 'DELETE');
}

// 资源管理
export async function patentList(params) {
  return request(`${config.serveraddress}/resource/patent/list`, params, 'GET');
}
export async function patentInfo(params) {
  return request(`${config.serveraddress}/resource/patent/info`, params, 'GET');
}
export async function expertsList(params) {
  return request(`${config.serveraddress}/resource/experts/list`, params, 'GET');
}
export async function expertsInfo(params) {
  return request(`${config.serveraddress}/resource/experts/info`, params, 'GET');
}
export async function enterpriseList(params) {
  return request(`${config.serveraddress}/resource/enterprise/list`, params, 'GET');
}
export async function enterpriseInfo(params) {
  return request(`${config.serveraddress}/resource/enterprise/info`, params, 'GET');
}
export async function achievementsList(params) {
  return request(`${config.serveraddress}/resource/achievements/list`, params, 'GET');
}
export async function achievementsInfo(params) {
  return request(`${config.serveraddress}/resource/achievements/info`, params, 'GET');
}

// export async function matchEnterpriseTag() {
//   return request(`${config.serveraddress}/match/enterprise/tag`, params, 'GET');
// }

// 匹配管理
export async function matchEnterpriseList(params) {
  return request(`${config.serveraddress}/match/enterprise/list`, params, 'GET');
}
export async function matchEnterpriseTag(params) {
  return request(`${config.serveraddress}/match/enterprise/tag`, params, 'GET');
}
export async function matchEnterpriseTagAdd(params) {
  return request(`${config.serveraddress}/match/enterprise/tag/add`, params, 'POST');
}
export async function matchEnterpriseTagDelete(params) {
  return request(`${config.serveraddress}/match/enterprise/tag/delete`, params, 'DELETE');
}

export async function matchRequestList(params) {
  return request(`${config.serveraddress}/match/request/list`, params, 'GET');
}
export async function matchRequestTag(params) {
  return request(`${config.serveraddress}/match/request/tag`, params, 'GET');
}
export async function matchRequestTagAdd(params) {
  return request(`${config.serveraddress}/match/request/tag/add`, params, 'POST');
}
export async function matchRequestTagDelete(params) {
  return request(`${config.serveraddress}/match/request/tag/delete`, params, 'DELETE');
}

export async function matchAchievementsReport(params) {
  return request(`${config.serveraddress}/match/achievements/report`, params, 'GET');
}
export async function matchExpertReport(params) {
  return request(`${config.serveraddress}/match/expert/report`, params, 'GET');
}
export async function matchPatentReport(params) {
  return request(`${config.serveraddress}/match/patent/report`, params, 'GET');
}

// 系统管理-会员管理
export async function memberList(params) {
  return request(`${config.serveraddress}/member/list`, params, 'POST');
}

export async function memberAudit(params) {
  return request(`${config.serveraddress}/member/audit`, params, 'PUT');
}

export async function memberAuthInfo(params) {
  return request(`${config.serveraddress}/member/authInfo`, params, 'GET');
}

// 项目管理
export async function projectDeclareAudit(params) {
  return request(`${config.serveraddress}/project/declare/audit`, params, 'PUT');
}

export async function projectDeclareDetail(params) {
  return request(`${config.serveraddress}/project/declare/detail`, params, 'GET');
}

export async function projectDeclareList(params) {
  return request(`${config.serveraddress}/project/declare/list`, params, 'POST');
}

export async function projectDeclareReview(params) {
  return request(`${config.serveraddress}/project/declare/review`, params, 'PUT');
}

export async function projectResultList(params) {
  return request(`${config.serveraddress}/project/result/list`, params, 'POST');
}

export async function projectReviewList(params) {
  return request(`${config.serveraddress}/project/review/list`, params, 'POST');
}

// 专家管理
export async function expertAdd(params) {
  return request(`${config.serveraddress}/expert/add`, params, 'POST');
}

export async function expertList(params) {
  return request(`${config.serveraddress}/expert/list`, params, 'POST');
}

export async function expertDelete(params) {
  return request(`${config.serveraddress}/expert/delete`, params, 'DELETE');
}

export async function expertUpdate(params) {
  return request(`${config.serveraddress}/expert/update`, params, 'PUT');
}

// 项目公示管理
export async function publicityAdd(params) {
  return request(`${config.serveraddress}/publicity/add`, params, 'POST');
}

export async function publicityList(params) {
  return request(`${config.serveraddress}/publicity/list`, params, 'GET');
}

export async function publicityDelete(params) {
  return request(`${config.serveraddress}/publicity/delete`, params, 'DELETE');
}

export async function publicityUpdate(params) {
  return request(`${config.serveraddress}/publicity/update`, params, 'PUT');
}

// index dashboard
export async function dashBoard(params) {
  return request(`${config.serveraddress}/index/dashboard`, params, 'POST');
}

// seo
export async function seoList(params) {
  return request(`${config.serveraddress}/system/seo/list`, params, 'GET');
}

export async function seoAdd(params) {
  return request(`${config.serveraddress}/system/seo/add`, params, 'POST');
}

export async function seoUpdate(params) {
  return request(`${config.serveraddress}/system/seo/update`, params, 'PUT');
}

export async function seoDelete(params) {
  return request(`${config.serveraddress}/system/seo/delete`, params, 'DELETE');
}

// role
export async function roleList(params) {
  return request(`${config.serveraddress}/system/role/list`, params, 'GET');
}

export async function roleAdd(params) {
  return request(`${config.serveraddress}/system/role/add`, params, 'POST');
}

export async function roleDistribution(params) {
  return request(`${config.serveraddress}/system/role/distribution`, params, 'POST');
}

export async function roleUpdate(params) {
  return request(`${config.serveraddress}/system/role/update`, params, 'PUT');
}

export async function roleDelete(params) {
  return request(`${config.serveraddress}/system/role/delete`, params, 'DELETE');
}

export async function permissionList(params) {
  return request(`${config.serveraddress}/system/role/permissionList`, params, 'GET');
}

export async function permissionListByRole(params) {
  return request(`${config.serveraddress}/system/role/permissionListByRole`, params, 'GET');
}

// log
export async function logList(params) {
  return request(`${config.serveraddress}/system/log/list`, params, 'GET');
}

// setting
export async function settingList(params) {
  return request(`${config.serveraddress}/system/config/list`, params, 'GET');
}

export async function settingAdd(params) {
  return request(`${config.serveraddress}/system/config/add`, params, 'POST');
}

export async function settingUpdate(params) {
  return request(`${config.serveraddress}/system/config/update`, params, 'PUT');
}

export async function settingDelete(params) {
  return request(`${config.serveraddress}/system/config/delete`, params, 'DELETE');
}

// admin user
export async function adminList(params) {
  return request(`${config.serveraddress}/system/admin/list`, params, 'GET');
}

export async function adminAdd(params) {
  return request(`${config.serveraddress}/system/admin/add`, params, 'POST');
}

export async function adminUpdate(params) {
  return request(`${config.serveraddress}/system/admin/update`, params, 'PUT');
}

export async function adminDelete(params) {
  return request(`${config.serveraddress}/system/admin/delete`, params, 'DELETE');
}

// payment order
export async function orderList(params) {
  return request(`${config.serveraddress}/business/order/list`, params, 'POST');
}

export async function orderDetail(params) {
  return request(`${config.serveraddress}/business/order/detail`, params, 'POST2');
}

export async function orderCancel(params) {
  return request(`${config.serveraddress}/business/order/cancel`, params, 'PUT');
}

export async function orderConfirm(params) {
  return request(`${config.serveraddress}/business/order/confirm`, params, 'PUT');
}

export async function orderPayment(params) {
  return request(`${config.serveraddress}/business/order/payment`, params, 'PUT');
}

export async function orderRefund(params) {
  return request(`${config.serveraddress}/business/order/refund`, params, 'PUT');
}

export async function orderRequest(params) {
  return request(`${config.serveraddress}/business/order/request`, params, 'PUT');
}

export async function orderSend(params) {
  return request(`${config.serveraddress}/business/order/send`, params, 'PUT');
}

export async function requestConfirm(params) {
  return request(`${config.serveraddress}/business/order/requestConfirm`, params, 'PUT');
}

export async function orderExport(params) {
  return fileDownload(`${config.serveraddress}/business/order/export`, params, '订单');
}

// service order
export async function serviceList(params) {
  return request(`${config.serveraddress}/business/service/list`, params, 'POST');
}

export async function serviceReply(params) {
  return request(`${config.serveraddress}/business/service/reply`, params, 'PUT');
}

// demand
export async function demandList(params) {
  return request(`${config.serveraddress}/business/demand/list`, params, 'POST');
}

export async function demandReply(params) {
  return request(`${config.serveraddress}/business/demand/reply`, params, 'PUT');
}

// selling
export async function sellingList(params) {
  return request(`${config.serveraddress}/business/selling/list`, params, 'POST');
}

export async function sellingReply(params) {
  return request(`${config.serveraddress}/business/selling/reply`, params, 'PUT');
}

// package
export async function packageGrant(params) {
  return request(`${config.serveraddress}/business/package/grant`, params, 'POST');
}

export async function packageList(params) {
  return request(`${config.serveraddress}/business/package/list`, params, 'POST');
}

// member
// export async function memberList(params) {
//   return request(`${config.serveraddress}/management/member/list`, params, 'POST');
// }

export async function memberUpdate(params) {
  return request(`${config.serveraddress}/management/member/update`, params, 'PUT');
}

export async function memberExport(params) {
  return fileDownload(`${config.serveraddress}/management/member/export`, params, '会员');
}

// export async function memberAudit(params) {
//   return request(`${config.serveraddress}/management/member/audit`, params, 'PUT');
// }

// export async function memberAuthInfo(params) {
//   return request(`${config.serveraddress}/management/member/authInfo`, params, 'GET');
// }

export async function memberAttentionList(params) {
  return request(`${config.serveraddress}/management/member/attentionList`, params, 'POST');
}

export async function memberMonitorList(params) {
  return request(`${config.serveraddress}/management/member/monitorList`, params, 'POST');
}

export async function memberPackageList(params) {
  return request(`${config.serveraddress}/management/member/packageList`, params, 'GET');
}

// level
export async function levelList(params) {
  return request(`${config.serveraddress}/management/level/list`, params, 'GET');
}

export async function levelAdd(params) {
  return request(`${config.serveraddress}/management/level/add`, params, 'POST');
}

export async function levelUpdate(params) {
  return request(`${config.serveraddress}/management/level/update`, params, 'PUT');
}

// patent
// export async function patentList(params) {
//   return request(`${config.serveraddress}/management/patent/list`, params, 'GET');
// }

export async function getFeeDetail(params) {
  return request(`${config.serveraddress}/management/patent/getFeeDetail`, params, 'GET');
}

export async function syncPatentInfo(params) {
  return request(`${config.serveraddress}/management/patent/syncPatentInfo`, params, 'POST');
}

// service
export async function serviceConfigList(params) {
  return request(`${config.serveraddress}/management/service/list`, params, 'GET');
}

export async function serviceConfigAdd(params) {
  return request(`${config.serveraddress}/management/service/add`, params, 'POST');
}

export async function serviceConfigUpdate(params) {
  return request(`${config.serveraddress}/management/service/update`, params, 'PUT2');
}

export async function serviceConfigDelete(params) {
  return request(`${config.serveraddress}/management/service/delete`, params, 'DELETE');
}

// package
export async function packageConfigList(params) {
  return request(`${config.serveraddress}/management/package/list`, params, 'GET');
}

export async function packageConfigAdd(params) {
  return request(`${config.serveraddress}/management/package/add`, params, 'POST');
}

export async function packageConfigUpdate(params) {
  return request(`${config.serveraddress}/management/package/update`, params, 'PUT');
}

// content
export async function contentList(params) {
  return request(`${config.serveraddress}/management/content/list`, params, 'GET');
}

export async function contentAdd(params) {
  return request(`${config.serveraddress}/management/content/add`, params, 'POST');
}

export async function contentUpdate(params) {
  return request(`${config.serveraddress}/management/content/update`, params, 'PUT2');
}

// article
export async function articleList(params) {
  return request(`${config.serveraddress}/management/article/list`, params, 'GET');
}

export async function articleAdd(params) {
  return request(`${config.serveraddress}/management/article/add`, params, 'POST');
}

export async function articleUpdate(params) {
  return request(`${config.serveraddress}/management/article/update`, params, 'PUT2');
}

// eva config
export async function getByParentCode(params) {
  return request(`${config.serveraddress}/management/evalConfig/getByParentCode`, params, 'GET');
}

export async function getProvince(params) {
  return request(`${config.serveraddress}/management/evalConfig/getProvince`, params, 'GET');
}

export async function evalConfigList(params) {
  return request(`${config.serveraddress}/management/evalConfig/list`, params, 'GET');
}

export async function evalConfigSave(params) {
  return request(`${config.serveraddress}/management/evalConfig/save`, params, 'POST');
}

// eva list
export async function evalResultDetail(params) {
  return request(`${config.serveraddress}/business/evalResult/detail`, params, 'GET');
}

export async function evalResultList(params) {
  return request(`${config.serveraddress}/business/evalResult/list`, params, 'GET');
}

export async function evalResultReport(params) {
  return request(`${config.serveraddress}/business/evalResult/report`, params, 'GET');
}

export async function queryNotices() {
  return request('/api/notices');
}
export async function noticeDelete() {
  return request('/api/notices');
}
export async function noticeSave() {
  return request('/api/notices');
}
